package com.pkx.guide.info.service.impl;

import com.pkx.guide.common.util.StringUtils;
import com.pkx.guide.fcms.service.FcmsPlayService;
import com.pkx.guide.info.entity.TInfoGuide;
import com.pkx.guide.info.entity.TInfoGuidePlayItem;
import com.pkx.guide.info.entity.TInfoGuidePlayList;
import com.pkx.guide.info.entity.TInfoGuidePlayListDevice;
import com.pkx.guide.info.mapper.TInfoGuidePlayListMapper;
import com.pkx.guide.info.service.TInfoGuidePlayItemService;
import com.pkx.guide.info.service.TInfoGuidePlayListDeviceService;
import com.pkx.guide.info.service.TInfoGuidePlayListService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pkx.guide.info.service.TInfoGuideService;
import com.sansi.tools.RetrunData;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import java.text.DecimalFormat;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.transaction.annotation.Transactional;

/**
 * 诱导屏播放表信息表 服务实现类
 *
 * @author sunsy
 * @since 2023-11-24
 */
@Service
public class TInfoGuidePlayListServiceImpl extends ServiceImpl<TInfoGuidePlayListMapper, TInfoGuidePlayList> implements TInfoGuidePlayListService {

    @Resource
    private FcmsPlayService fcmsPlayService;

    @Resource
    private TInfoGuidePlayItemService playItemService;

    @Resource
    private TInfoGuideService guideService;

    @Resource
    private TInfoGuidePlayListDeviceService guidePlayListDeviceService;

    /**
     * 分页列表
     *
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    public IPage<TInfoGuidePlayList> pageList(Page page, TInfoGuidePlayList tInfoGuidePlayList) {
        LambdaQueryWrapper<TInfoGuidePlayList> lambdaQueryWrapper = new QueryWrapper<TInfoGuidePlayList>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuidePlayList.getTitle()), TInfoGuidePlayList::getTitle, tInfoGuidePlayList.getTitle());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuidePlayList.getName()), TInfoGuidePlayList::getName, tInfoGuidePlayList.getName());
        lambdaQueryWrapper.eq(tInfoGuidePlayList.getType() != null, TInfoGuidePlayList::getType, tInfoGuidePlayList.getType());
        lambdaQueryWrapper.eq(TInfoGuidePlayList::getDelFlg, 0);
        IPage<TInfoGuidePlayList> iPage = page(page, lambdaQueryWrapper);
        if (iPage.getRecords() != null && iPage.getRecords().size() > 0){
            TInfoGuidePlayItem playItem = null;
            TInfoGuidePlayListDevice playListDevice = null;
            for (TInfoGuidePlayList record : iPage.getRecords()) {
                // 查询播放项
                playItem = new TInfoGuidePlayItem();
                playItem.setPlayListId(record.getId());
                record.setGuidePlayItem(playItemService.selectByPlayListId(record.getId()));
                playListDevice = new TInfoGuidePlayListDevice();
                playListDevice.setPlayListId(record.getId());
                record.setGuidePlayListDeviceList(guidePlayListDeviceService.listByEntity(playListDevice));
            }
        }
        return iPage;
    }

    /**
     * 列表查询
     *
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    public List<TInfoGuidePlayList> listByEntity(TInfoGuidePlayList tInfoGuidePlayList) {
        LambdaQueryWrapper<TInfoGuidePlayList> lambdaQueryWrapper = new QueryWrapper<TInfoGuidePlayList>().lambda();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuidePlayList.getTitle()), TInfoGuidePlayList::getTitle, tInfoGuidePlayList.getTitle());
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(tInfoGuidePlayList.getName()), TInfoGuidePlayList::getName, tInfoGuidePlayList.getName());
        lambdaQueryWrapper.eq(tInfoGuidePlayList.getType() != null, TInfoGuidePlayList::getType, tInfoGuidePlayList.getType());
        lambdaQueryWrapper.eq(TInfoGuidePlayList::getDelFlg, 0);
        List<TInfoGuidePlayList> list = list(lambdaQueryWrapper);
        if (list != null && list.size() > 0){
            TInfoGuidePlayItem playItem = null;
            TInfoGuidePlayListDevice playListDevice = null;
            for (TInfoGuidePlayList record : list) {
                // 查询播放项
                playItem = new TInfoGuidePlayItem();
                playItem.setPlayListId(record.getId());
                record.setGuidePlayItem(playItemService.selectByPlayListId(record.getId()));
                playListDevice = new TInfoGuidePlayListDevice();
                playListDevice.setPlayListId(record.getId());
                record.setGuidePlayListDeviceList(guidePlayListDeviceService.listByEntity(playListDevice));
            }
        }
        return list;
    }

    /**
     * 详情
     *
     * @param id
     * @return
     */
    @Override
    public TInfoGuidePlayList selectById(Integer id) {
        TInfoGuidePlayList playList = getById(id);
        // 查询播放项
        TInfoGuidePlayItem playItem = new TInfoGuidePlayItem();
        playItem.setPlayListId(id);
        playList.setGuidePlayItem(playItemService.selectByPlayListId(id));
        TInfoGuidePlayListDevice playListDevice = new TInfoGuidePlayListDevice();
        playListDevice.setPlayListId(id);
        playList.setGuidePlayListDeviceList(guidePlayListDeviceService.listByEntity(playListDevice));
        return playList;
    }

    /**
     * 查询播放表数量
     *
     * @return
     */
    @Override
    public long getPlayListCount(){
        return count();
    }

    /**
     * 保存
     *
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveTInfoGuidePlayList(TInfoGuidePlayList tInfoGuidePlayList) {
        return save(tInfoGuidePlayList);
    }

    /**
     * 保存
     *
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public TInfoGuidePlayList savePlayList(TInfoGuidePlayList tInfoGuidePlayList){
        if (tInfoGuidePlayList.getGuidePlayItem() == null){
            throw new IllegalArgumentException("播放项不可为空");
        }
        // 生成播放表名称,不包含后缀
        DecimalFormat decimalFormat = new DecimalFormat("000");
        long PlayListCount = getPlayListCount() + 1;
        String name = decimalFormat.format(PlayListCount);
        tInfoGuidePlayList.setName(name);
        tInfoGuidePlayList = fcmsPlayService.createFcmsPlayList(tInfoGuidePlayList);
        saveOrUpdate(tInfoGuidePlayList);
        TInfoGuidePlayItem tInfoGuidePlayItem = tInfoGuidePlayList.getGuidePlayItem();
        tInfoGuidePlayItem.setPlayListId(tInfoGuidePlayList.getId());
        playItemService.saveOrUpdate(tInfoGuidePlayItem);
        if (tInfoGuidePlayList.getId() != null){
            // 删除关联关系
            guidePlayListDeviceService.deleteByPlayListId(tInfoGuidePlayList.getId());
        }
        // 上传至设备
        List<TInfoGuide> guideList = guideService.list(new QueryWrapper<TInfoGuide>().lambda().eq(TInfoGuide::getDelFlg, 0));
        if (guideList != null && guideList.size() > 0){
            for (TInfoGuide guide : guideList) {
                guide.setGuidePlayList(tInfoGuidePlayList);
                RetrunData retrunData = fcmsPlayService.fcmsUploadPlayList(guide);
                if (retrunData.getCode() == 0){
                    // 保存关联信息
                    TInfoGuidePlayListDevice guidePlayListDevice = new TInfoGuidePlayListDevice();
                    guidePlayListDevice.setPlayListId(tInfoGuidePlayList.getId());
                    guidePlayListDevice.setGuideId(guide.getId());
                    guidePlayListDevice.setGuideIp(guide.getGuidIp());
                    guidePlayListDeviceService.saveTInfoGuidePlayListDevice(guidePlayListDevice);
                }
            }
        }
        return tInfoGuidePlayList;
    }

    /**
     * 修改
     *
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean modify(TInfoGuidePlayList tInfoGuidePlayList) {
        return updateById(tInfoGuidePlayList);
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Integer id) {
        if (id == null){
            return false;
        }
        LambdaUpdateWrapper<TInfoGuidePlayList> lambdaUpdateWrapper = new UpdateWrapper<TInfoGuidePlayList>().lambda()
            .eq(TInfoGuidePlayList::getId, id)
            .set(TInfoGuidePlayList::getDelFlg, 1);
        update(lambdaUpdateWrapper);
        playItemService.deleteByPlayListId(id);
        return true;
    }

    /**
     * 向显示屏设备上传播放表
     * @param tInfoGuidePlayList
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean uploadPlayList(TInfoGuidePlayList tInfoGuidePlayList){
        if (tInfoGuidePlayList == null || tInfoGuidePlayList.getId() == null){
            throw new IllegalArgumentException("参数为空");
        }
        if (tInfoGuidePlayList.getGuidePlayListDeviceList() == null || tInfoGuidePlayList.getGuidePlayListDeviceList().size() == 0){
            throw new IllegalArgumentException("未选择情报板");
        }
        // 查询播放项
        TInfoGuidePlayItem guidePlayItem = playItemService.selectByPlayListId(tInfoGuidePlayList.getId());
        if (guidePlayItem == null){
            throw new RuntimeException("未查询到播放项信息");
        }
        tInfoGuidePlayList.setGuidePlayItem(guidePlayItem);
        for (TInfoGuidePlayListDevice item : tInfoGuidePlayList.getGuidePlayListDeviceList()) {
            // 查询设备信息
            TInfoGuide guide = guideService.selectById(item.getGuideId());
            if (guide == null){
                throw new RuntimeException("未查询到情报板信息，ID:" + item.getGuideId());
            }
            guide.setGuidePlayList(tInfoGuidePlayList);
            RetrunData retrunData = fcmsPlayService.fcmsUploadPlayList(guide);
            if (retrunData.getCode() != 0){
                throw new RuntimeException("播放表上传情报板失败，ID:" + item.getGuideId());
            }
            item.setPlayListId(tInfoGuidePlayList.getId());
            item.setGuideIp(guide.getGuidIp());
            guidePlayListDeviceService.saveTInfoGuidePlayListDevice(item);
        }
        return true;
    }
}
